/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import helper.Utilities;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import midlet.MainMidlet;
import util.SpriteClass;

/**
 *
 * @author aisyah
 */
public class ListSuratCanvas extends Canvas implements CommandListener {

    public int stateManager;
    private MainMidlet midlet;
    private Displayable displayable;
    int index = 0;
    public LayerManager lmng = new LayerManager();
    private String[] menu;
    private String language;

    public ListSuratCanvas(MainMidlet midlet) {
        this.midlet = midlet;
    }

    public ListSuratCanvas(MainMidlet midlet, Displayable displayable, String language) {
        this.midlet = midlet;
        this.displayable = displayable;
        this.language = language;
        System.out.println("language =" + language);
        if (language.equalsIgnoreCase("arb")) {
            this.menu = Utilities.itemSrtsArab;
        } else {
            this.menu = Utilities.itemSrts;
        }
    }

    protected void paint(Graphics g) {

        setFullScreenMode(true);
        lmng.paint(g, 0, 0);
        try {
            setFullScreenMode(true);
            // set background image
            SpriteClass bg = new SpriteClass(Image.createImage("/res/bg_surat.png"));
            bg.setPosition(0, 0);

            SpriteClass scrll_bar = new SpriteClass(Image.createImage("/res/scroll/scroll_bar.png"));
            scrll_bar.setPosition(215, 80);


            LayerManager lmng = new LayerManager();

//            lmng.append(scrll);
            lmng.append(scrll_bar);
            lmng.append(bg);
            lmng.paint(g, 0, 0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        int k = 90;

        g.setColor(0, 0, 0);
        int index2;
        Image panah;
        Image scroll;

        switch (index) {
            case 0:
                index2 = index;
                System.out.println("Case 0 index2 =" + index2);
                System.out.println("Case 0 index =" + index);
                System.out.println("=========================");
                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 110, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, 96, Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                        try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    // index2 =(index2+1);
                }
                break;
            case 1:
                System.out.println("Case1");
                System.out.println("index =" + index);
                System.out.println("=========================");
                index2 = 0;
                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 140, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, 96, Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                       
//              try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    //index2 =(index2+1);
                }
                break;

            case 2:
                System.out.println("Case2");
                System.out.println("index =" + index);
                System.out.println("=========================");
                index2 = 0;
                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 170, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, 96, Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                        try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    //index2 =(index2+1);
                }
                break;
            case 3:
                System.out.println("Case3");
                System.out.println("index =" + index);
                System.out.println("=========================");
                index2 = 0;
                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 200, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, 96, Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                        try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    //index2 =(index2+1);
                }
                break;
            case 4:
                System.out.println("Case4");
                System.out.println("index =" + index);
                System.out.println("=========================");
                index2 = 0;
                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 230, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, 96, Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                        try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    //index2 =(index2+1);
                }
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
            case 26:
            case 27:
            case 28:
            case 29:
            case 30:
            case 31:
            case 32:
            case 33:
            case 34:
            case 35:
            case 36:
            case 37:
            case 38:
            case 39:
            case 40:
            case 41:
            case 42:
            case 43:
            case 44:
            case 45:
            case 46:
            case 47:
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
            case 58:
            case 59:
            case 60:
            case 61:
            case 62:
            case 63:
            case 64:
            case 65:
            case 66:
            case 67:
            case 68:
            case 69:
            case 70:
            case 71:
            case 72:
            case 73:
            case 74:
            case 75:
            case 76:
            case 77:
            case 78:
            case 79:
            case 80:
            case 81:
            case 82:
            case 83:
            case 84:
            case 85:
            case 86:
            case 87:
            case 88:
            case 89:
            case 90:
            case 91:
            case 92:
            case 93:
            case 94:
            case 95:
            case 96:
            case 97:
            case 98:
            case 99:
            case 100:
            case 101:
            case 102:
            case 103:
            case 104:
            case 105:
            case 106:
            case 107:
            case 108:
            case 109:
            case 110:
            case 111:
            case 112:
            case 113:
            case 114:
                index2 = index - 5;
                System.out.println("13 Case index2 =" + index2);
                System.out.println("13 Case index =" + index);
                System.out.println("=========================");

                try {
                    panah = Image.createImage("/res/slider.png");
                    g.drawImage(panah, 25, 260, Graphics.LEFT | Graphics.BOTTOM);

                    scroll = Image.createImage("/res/scroll/scroll_c.png");
                    g.drawImage(scroll, 215, (int) (96 + index2 * (1.5)), Graphics.LEFT | Graphics.BOTTOM);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                for (int i = 0; i < 6; i++) {
                    if (language.equals("arb")) {
//                        try {
//                            g.drawString(StringUtil.getPrintArab(menu[(index2 + i)]), 120, k, Graphics.TOP | Graphics.HCENTER);
//                        } catch (IOException ex) {
//                            ex.printStackTrace();
//                        }
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    } else {
                        g.drawString(menu[(index2 + i)], 120, k, Graphics.TOP | Graphics.HCENTER);
                    }
                    k += 30;
                    //index2 =(index2+1);
                }
                break;
        }
    }

    protected void keyPressed(int keyCode) {

        if (keyCode == -1) {
            // atas
            if (index > 0) {
                index = index - 1;
            }
        } else if (keyCode == -2) {
            // bawah
            if (index < menu.length - 1) {
                index = index + 1;
            }
        } else if (keyCode == -3) {
            // kiri
            index -= 1;
        } else if (keyCode == -4) {
            // kanan
            index += 1;
        } else if (keyCode == -5) {
            // fire button
            System.out.println("=======>>select content index=" + index);
        } else if (keyCode == -6) {
            Display.getDisplay(midlet).setCurrent(displayable);
        } else if (keyCode == -7) {
            this.midlet.destroyApp(true);
        }
        repaint();
    }

    public void commandAction(Command c, Displayable d) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
}
